<template>
  <div class="app-container">
    <el-row class="mb20">
      <el-form :model="queryParams" :inline="true" ref="queryForm">
        <el-form-item label="ID" prop="identificationd">
          <el-input v-model="queryParams.identificationd" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="开门时间" prop="openTime">
          <el-date-picker v-model="queryParams.openTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" @change="changeTime" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
          </el-button
          >
        </el-form-item>
      </el-form>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="ID" prop="identification"/>
      <el-table-column label="姓名" prop="name">
        <template slot-scope="scope">
          {{ scope.row.name? scope.row.name : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="开门方式" prop="unlockType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.unlock_type" :value="scope.row.unlockType" />
        </template>
      </el-table-column>
      <el-table-column label="开门时间" prop="openTime">
        <template slot-scope="scope">
          {{ scope.row.unlockTime | formatDate('YYYY-MM-DD HH:mm') }}
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"
    />
  </div>

</template>

<script>
import { getOpenDoorRecord } from '@/api/smartLock/lockManage'
import { compareDate } from '@/utils/common/moment'

export default {
  name: 'external-open-record',
  dicts:['unlock_type'],
  props: {
    data: {
      default: undefined
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      total: 0,
      queryParams: {
        lockId:this.data.lockId,
        identificationd:'',
        startTime:'',
        endTime:'',
        unlockType:'',
        openTime: [],
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  computed: {
    pickerOptions(){
      return {
        disabledDate: (time) => {
          return compareDate(time, this.data.createTime,'<')
        }
      }
    }
  },
  created() {
    console.log('created')
    this.getList()
  },
  methods: {
    handleSearch() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleSearch()
    },

    getList() {
      this.loading = true
      let params = {
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        lockId: this.data.lockId,
        identificationd: this.queryParams.identificationd,
        startTime: this.queryParams.startTime,
        endTime: this.queryParams.endTime,
        unlockType: '',
      }
      getOpenDoorRecord(params).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.list = []
        this.total = 0
        this.loading = false
      })
      // this.list = [
      //   {
      //     name: '张三',
      //     openType: 1,
      //     openTime: '2021-01-01 10:00:00'
      //   },
      //   {
      //     name: '李四',
      //     openType: 1,
      //     openTime: '2021-01-01 10:00:00'
      //   }
      // ]
      // this.total = 2
      // this.loading = false
    },
    // 时间回调
    changeTime(val) {
      console.log(val)
      if (val.length === 0) {
        this.queryParams.startTime = ''
        this.queryParams.endTime = ''
        return
      }
      this.queryParams.startTime = val[0]
      this.queryParams.endTime = val[1]
    }
  }
}
</script>

<style scoped lang="scss">

</style>
